package com.sky.aspect;

import com.sky.constant.MessageConstant;
import com.sky.exception.EmployeeNotLoginException;
import com.sky.exception.UserNotLoginException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 验证失败后抛出异常
 */
@Aspect
@Slf4j
@Component
public class JwtTokenAspect {
    /**
     * 切入点
     */
    @Pointcut("execution(boolean com.sky.interceptor.JwtTokenAdminInterceptor.preHandle(..))")
    public void JwtTokenAdminPointCut() {
    }

    @Pointcut("execution(boolean com.sky.interceptor.JwtTokenUserInterceptor.preHandle(..))")
    public void JwtTokenUserPointCut() {
    }

    /**
     * 切面，返回false后抛出异常
     */
    @AfterReturning(value = "JwtTokenAdminPointCut()", returning = "flag")
    public void afterAdminReturnThrowErr(boolean flag) {
        if (!flag) {
            throw new EmployeeNotLoginException(MessageConstant.EMPLOYEE_NOT_LOGIN);
        }
    }

    @AfterReturning(value = "JwtTokenUserPointCut()", returning = "flag")
    public void afterUserReturnThrowErr(boolean flag) {
        if (!flag) {
            throw new UserNotLoginException(MessageConstant.USER_NOT_LOGIN);
        }
    }
}
